package com.aaa.marketresearch.webclient.gwt.admin.widget;

import com.aaa.marketresearch.webclient.gwt.domain.FamilyMember;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Nail
 */
public class FamilyMemberCell extends Composite {

    interface FamilyMemberCellUiBinder extends UiBinder<Widget, FamilyMemberCell> {
    }

    private static FamilyMemberCellUiBinder uiBinder = GWT.create(FamilyMemberCellUiBinder.class);

    private static FamilyMemberCell instance = null;

    @UiField
    TextBox familyMembersName;

    @UiField
    ListBox familyMembersGender;

    @UiField
    ListBox familyMembersProfession;

    @UiField
    TextBox familyMembersBirthday;

    public static FamilyMemberCell getInstance() {
        if (instance == null) {
            instance = new FamilyMemberCell();
        }
        return instance;
    }

    private FamilyMember familyMember;

    public FamilyMember getFamilyMember() {
        this.familyMember.setName(familyMembersName.getText());
        this.familyMember.setGender(this.familyMembersGender.getValue(this.familyMembersGender.getSelectedIndex()));
        this.familyMember.setProfession(this.familyMembersProfession.getValue(this.familyMembersProfession.getSelectedIndex()));
        this.familyMember.setBirthday(familyMembersBirthday.getText());
        return this.familyMember;
    }

    public FamilyMemberCell() {
        // initialize
        initWidget(uiBinder.createAndBindUi(this));
    }

    public void setFamilyMember(FamilyMember familyMember) {
        this.familyMember = familyMember;

        familyMembersName.setText(this.familyMember.getName());

        {
            int n = this.familyMembersGender.getItemCount();
            this.familyMembersGender.setSelectedIndex(0);
            for (int i = 0; i < n; i++) {
                String value = this.familyMembersGender.getValue(i);
                if (value.equalsIgnoreCase(this.familyMember.getGender())) {
                    this.familyMembersGender.setSelectedIndex(i);
                    break;
                }
            }
        }

        {
            int n = this.familyMembersProfession.getItemCount();
            this.familyMembersProfession.setSelectedIndex(0);
            for (int i = 0; i < n; i++) {
                String value = this.familyMembersProfession.getValue(i);
                if (value.equalsIgnoreCase(this.familyMember.getProfession())) {
                    this.familyMembersProfession.setSelectedIndex(i);
                    break;
                }
            }
        }

        familyMembersBirthday.setText(this.familyMember.getTextBirthday());

    }

}
